﻿Imports Testeitor.DAL
Imports System.Data.SqlClient
Imports Testeitor.Entidades

Public MustInherit Class DAOBase
    Private conexion As SqlConnection
    Private listaID As Stack(Of Integer)
    Private count As Integer

    Sub New()
        Me.conexion = DALSQLServer.Instancia.Conexion
        Me.listaID = New Stack(Of Integer)
    End Sub

    Public MustOverride Function commandActualizar(ByRef ent As EntityBase) As SqlCommand
    Public MustOverride Function commandEliminar(ByVal id As Integer) As SqlCommand
    Public MustOverride Function commandInsertar(ByRef ent As EntityBase) As SqlCommand
    Public MustOverride Function commandSeleccionar(ByVal id As Integer) As SqlCommand
    Public MustOverride Function commandSeleccionarWhere(ByVal where As String) As SqlCommand
    Public MustOverride Function commandSeleccionarTodas() As SqlCommand
    Public MustOverride Function CargarEntidad(ByVal reader As IDataReader, ByRef lista As Stack(Of Integer)) As EntityBase
    Public MustOverride Sub CargarRelacion(ByRef entidad As EntityBase, ByRef stack As Stack(Of Integer))

    Public Sub Actualizar(ByVal entidad As Entidades.EntityBase)
        'Abro la conexion
        conexion.Open()
        'Creo el Command con la consulta
        Dim cmd = Me.commandActualizar(entidad)
        cmd.Connection = Me.conexion
        'Ejecuto la consulta
        cmd.ExecuteReader()
        'Cierro la conexion
        conexion.Close()
    End Sub

    Public Sub Eliminar(ByVal id As Integer)
        'Abro la conexion
        conexion.Open()
        'Creo el Command con la consulta
        Dim cmd = Me.commandEliminar(id)
        cmd.Connection = Me.conexion
        'Ejecuto la consulta
        cmd.ExecuteReader()
        'Cierro la conexion
        conexion.Close()
    End Sub

    Public Sub Insertar(ByVal entidad As Entidades.EntityBase)
        'Abro la conexion
        Me.conexion.Open()
        'Creo el Command con la consulta
        Dim cmd = Me.commandInsertar(entidad)
        cmd.Connection = Me.conexion
        'Ejecuto la consulta
        cmd.ExecuteReader()
        'Cierro la conexion
        conexion.Close()
    End Sub

    Public Function Seleccionar(ByVal id As Integer) As Entidades.EntityBase

        If conexion.State = ConnectionState.Closed Then
            'Abro la conexion
            conexion.Open()
        End If

        Dim cmd = Me.commandSeleccionar(id)
        cmd.Connection = Me.conexion

        Dim reader As IDataReader
        'Ejecuto la consulta
        reader = cmd.ExecuteReader()

        Dim entidad As New EntityBase
        entidad = CargarEntidad(reader, listaID)
        'Cierro la conexion
        conexion.Close()

        Me.CargarRelacion(entidad, listaID)

        Return entidad
    End Function

    Public Function SeleccionarWhere(ByVal where As String) As System.Collections.Generic.List(Of Entidades.EntityBase)
        'Abro la conexion
        conexion.Open()
        Dim cmd = Me.commandSeleccionarWhere(where)
        cmd.Connection = Me.conexion

        Dim reader As IDataReader
        'Ejecuto la consulta
        reader = cmd.ExecuteReader()

        Dim lista As List(Of EntityBase)
        lista = Cargar(reader)
        'Cierro la conexion
        conexion.Close()

        For Each ent As EntityBase In lista
            Me.CargarRelacion(ent, Me.listaID)
        Next

        Return lista
    End Function

    Public Function SeleccionarTodas() As System.Collections.Generic.List(Of Entidades.EntityBase)
        'Abro la conexion
        conexion.Open()
        Me.count = 0
        'Creo el Command con la consulta
        Dim cmd = commandSeleccionarTodas()
        cmd.Connection = Me.conexion
        Dim reader As IDataReader
        'Ejecuto la consulta
        reader = cmd.ExecuteReader()

        Dim lista As List(Of EntityBase)
        lista = Cargar(reader)
        'Cierro la conexion
        conexion.Close()

        For Each ent As EntityBase In lista
            Me.CargarRelacion(ent, Me.listaID)
        Next

        Return lista
    End Function

    Public Function Cargar(ByVal reader As IDataReader) As List(Of EntityBase)
        'Creo la lista vacia
        Dim lista As New List(Of EntityBase)
        'Itero en el data reader y cargo los datos basicos
        While reader.Read()
            lista.Add(CargarEntidad(reader, Me.listaID))
            Me.count = Me.count + 1
        End While

        reader.Close()
        Return lista
    End Function

End Class
